jsx3.Class.defineClass(
        "tibco.admin.monitormanage.uicomponents.misc.serviceSearch.ServiceSearch", //the full name of the class to create
        jsx3.gui.Block, //  extends
        null, // implements
        function (plugin) {

            plugin.isServiceInstance = false;
            plugin.serviceSearchSubject = "com.tibco.admin.monitormanage.uicomponents.misc.serviceSearch.ServiceSearch.serviceSearch";
            plugin.serviceSearchSubjectForBrowse = "com.tibco.admin.monitormanage.uicomponents.misc.serviceSearch.ServiceSearch.serviceSearchForBrowse";
            plugin.browseParams = "";
            plugin.flag = true;
            plugin.prototype.init = function() {
                this.jsxsuper();
            }

            plugin.prototype.getNamespace = function() {
                return "tibco.admin";
            }

            plugin.prototype.setUpComponent = function() {
                tibco.admin.monitormanage.general.PageBusUtils.subscribe(tibco.admin.monitormanage.general.NavigationHandler.mainPageDisplayTopic, this.getServiceInstances, this);
            }


            plugin.prototype.getServiceInstances = function(subject, message, obj) {
                plugin.browseParams = ""
                if (message.screenIdVal == tibco.admin.monitormanage.uicomponents.page.serviceView.ServiceView.searchServicesScreenId && message.isFrmBrowse)
                {
                    plugin.isServiceInstance = message.isServiceInstance;
                    if (plugin.isServiceInstance) {
                        plugin.browseParams = { initialized:true,isServiceInstance: plugin.isServiceInstance,environmentId:message.environmentId,machineId:message.machineId,
                            nodeId:message.nodeId};
                    }
                    else {
                        plugin.browseParams = { initialized:true,isServiceInstance: plugin.isServiceInstance,environmentId:message.environmentId,serviceAssemblyId:message.serviceAssemblyId,
                            serviceUnitId:message.serviceUnitId};
                    }
                    tibco.admin.monitormanage.general.PageBusUtils.publish(plugin.serviceSearchSubject, plugin.browseParams);
                }
                else if (message.screenIdVal == tibco.admin.monitormanage.uicomponents.page.serviceView.ServiceView.searchServicesScreenId && !message.isFrmBrowse)
                {
                    plugin.isServiceInstance = false;
                }

                if (tibco.admin.monitormanage.uicomponents.table.services.Services && plugin.flag) {
                    plugin.flag = false;
                    topics = tibco.admin.monitormanage.uicomponents.table.services.Services.searchParametersSubject;
                    tibco.admin.monitormanage.general.PageBusUtils.subscribe(topics, obj.retrieveSearchParameters, obj);
                }
            }

            plugin.prototype.retrieveSearchParameters = function(subject, message, obj) {
                var searchParams = obj.getSearchParameters();
                var msg;
                if (plugin.browseParams) {
                    msg = { initialized:true, name:searchParams.name,resourceType:searchParams.resourceType,
                        interfaceName:searchParams.interfaceName,namespace:searchParams.namespace,isServiceInstance: plugin.isServiceInstance,
                        nodeId:plugin.browseParams.nodeId,machineId:plugin.browseParams.machineId,environmentId:plugin.browseParams.environmentId,
                        serviceAssemblyId:plugin.browseParams.serviceAssemblyId, serviceUnitId:plugin.browseParams.serviceUnitId};
                } else
                {
                    msg = { initialized:true,name:searchParams.name,resourceType:searchParams.resourceType,interfaceName:searchParams.interfaceName,namespace:searchParams.namespace,isServiceInstance: plugin.isServiceInstance};
                }

                tibco.admin.monitormanage.general.PageBusUtils.publish(plugin.serviceSearchSubject, msg);

            }


            plugin.prototype.btnSearch_onclick = function() {
                this.getDescendantOfName("noResultsLbl_MM_svc").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);  
                this.getDescendantOfName("noResultsImg_MM_svc").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                topics = tibco.admin.monitormanage.uicomponents.table.services.Services.serviceSearchResultSubject;
                tibco.admin.monitormanage.general.PageBusUtils.subscribe(topics, this.getNoofServiceSearhRecords, this);

                imagePath = "tibco/admin/monitormanage/images/roller.gif";
                var statusText = "<span style='width:100px'><img src=\"" + tibco.admin.Main.resolveURI(this.getNamespace(), imagePath) + "\" height=\"16px\" width=\"16px\"> &nbsp;" + tibco.admin.Main.getServer().getDynamicProperty("@MM@general@searchingLabel") + "</span>";
                this.getDescendantOfName("searchingLbl").setText(statusText);
                this.getDescendantOfName("searchingLbl").repaint();


                this.getDescendantOfName("searchBtn_MM_svc").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                this.getDescendantOfName("searchingBlock").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                this.getDescendantOfName("cancelBtn_MM_svc").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                var searchParams = this.getSearchParameters();
                var message;
                if (plugin.browseParams) {
                    message = { initialized:true, name:searchParams.name,resourceType:searchParams.resourceType,
                        interfaceName:searchParams.interfaceName,namespace:searchParams.namespace,isServiceInstance: plugin.isServiceInstance,
                        nodeId:plugin.browseParams.nodeId,machineId:plugin.browseParams.machineId,environmentId:plugin.browseParams.environmentId,
                        serviceAssemblyId:plugin.browseParams.serviceAssemblyId, serviceUnitId:plugin.browseParams.serviceUnitId};
                } else
                {
                    message = { initialized:true,name:searchParams.name,resourceType:searchParams.resourceType,interfaceName:searchParams.interfaceName,namespace:searchParams.namespace,isServiceInstance: plugin.isServiceInstance};
                }
                tibco.admin.monitormanage.general.PageBusUtils.publish(plugin.serviceSearchSubject, message);


            }

            plugin.prototype.btnCancel_onclick = function() {
                this.getDescendantOfName("searchBtn_MM_svc").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                this.getDescendantOfName("searchingBlock").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                this.getDescendantOfName("cancelBtn_MM_svc").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
            }
            plugin.prototype.getNoofServiceSearhRecords = function(subject, message, obj) {
                
                if(message.searchResults>0){
                obj.getDescendantOfName("searchingBlock").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                obj.getDescendantOfName("searchBtn_MM_svc").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                obj.getDescendantOfName("cancelBtn_MM_svc").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                }
                else {
                obj.getDescendantOfName("searchingBlock").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                obj.getDescendantOfName("searchBtn_MM_svc").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                obj.getDescendantOfName("cancelBtn_MM_svc").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);  
                obj.getDescendantOfName("noResultsLbl_MM_svc").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                 obj.getDescendantOfName("noResultsImg_MM_svc").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);     
                }
            }

            plugin.prototype.getSearchParameters = function () {
                var searchParameters = {name:this.getDescendantOfName("srcCritNameTxtBx_MM_svc").getValue(),resourceType:this.getDescendantOfName("srcCritResourceTypeSelect_MM_svc").getValue(),
                    interfaceName:this.getDescendantOfName("srcCritInterfaceNameTxtBx_MM_svc").getValue() ,namespace:this.getDescendantOfName("srcCritNamespaceTxtBx_MM_svc").getValue() };
                return searchParameters;
            }

        });

